/*
 * balloonSplash.c
 *
 *  Created on: 24/mag/2011
 *      Author: Giosuè, Alfonso, Luca, Antonio
 */

#include "balloonSplash.h"
#include <time.h>
#define MAX_BALLOON 30

int main2(int argc, char* argv[]) {

	// Fase dichiarativa
	clock_t t_init, t_final;
	char st[100];
	char c = 0;
	int initial = atoi(argv[2]), last = atoi(argv[3]);

	cvNamedWindow("RobotEye", CV_WINDOW_AUTOSIZE);

	Balloon targets[MAX_BALLOON];
	Balloon obstacles[MAX_BALLOON];

	//sprintf(st, "%s/%d.jpg", argv[1], 1);
	//IplImage* src = cvLoadImage(st);

	IplImage* src = connectToCam(1);

	CvPoint origin = cvPoint((src->width)/2, src->height);

	//for (int i = initial; i < last && c != 27; i++) {
	while(true){
		t_init = clock();

		//sprintf(st, "%s/%d.jpg", argv[1], i);
		//IplImage* image = cvLoadImage(st);
		IplImage* image = connectToCam(0);

	    if (!image) {
	    	printf("\n No frame captured!\n");
	    	break;
	    }

		int num_target = getTargets(image, targets, origin);
		////int num_obstacle = getObstacles(image, obstacles, origin);

		CvScalar colourContours_g = CV_RGB(76,224,25);
		CvScalar colourContours_a = CV_RGB(255,0,0);

		nearest(targets, num_target);

		bool isYellows = isYellow(image,origin,144,128,10);

		drawEllispe(image, num_target, targets, colourContours_g);
		////drawEllispe(image, num_obstacle, obstacles, colourContours_a);


		if(isYellows)
			printf("E' giallo\n");
		else
			printf("Sono daltonico\n");



		cvShowImage("RobotEye", image);
		t_final = clock() - t_init;
		double cout = (double) t_final / ((double) CLOCKS_PER_SEC);
		printf("\ncount: %f sec", cout);


		c = cvWaitKey(25);
		//c = cvWaitKey(0);
		if(c==27){
			break;
		}
		//Rilascio delle risorse
		cvReleaseImage(&image);

	}

	cvDestroyAllWindows();
	return 0;
}
